import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_boost/flutter_boost.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart' as picker;
import 'package:image_picker/image_picker.dart';
import 'package:permission_handler/permission_handler.dart';
import '../../../widgets/yq_step_indicator.dart';

class OrganizationAuthController extends GetxController {
  // 步骤标题
  final List<String> steps = const [
    '基本信息',
    '经营信息',
    '组织信息',
    '财务信息',
  ];

  late final StepController stepController;
  late final PageController pageController;
  bool agreeProtocol = false;
  int currentPage = 0;

  // 机构图片列表 - 存储本地文件路径
  List<String> images = [];
  final _picker = picker.ImagePicker();

  String? selectedAddress;

  @override
  void onInit() {
    super.onInit();
    stepController = StepController(totalSteps: steps.length);
    pageController = PageController();
  }

  @override
  void onClose() {
    stepController.dispose();
    pageController.dispose();
    super.onClose();
  }

  void handleNextStep() {
    if (currentPage < 3) {
      pageController.nextPage(
        duration: const Duration(milliseconds: 300),
        curve: Curves.easeInOut,
      );
      stepController.nextStep();
      currentPage++;
      update();
    }
  }

  void toggleAgreeProtocol(bool? value) {
    agreeProtocol = value ?? false;
    update();
  }

  void handleSubmitAuth() {
    // TODO: 处理提交认证逻辑
    update();
  }

  void onPageChanged(int index) {
    currentPage = index;
    stepController.jumpToStep(index);
    update();
  }

  // 检查权限
  Future<bool> _checkPermission(ImageSource source) async {
    if (source == ImageSource.camera) {
      final status = await Permission.camera.request();
      if (status.isGranted) {
        return true;
      }
    } else {
      final status = await Permission.photos.request();
      if (status.isGranted) {
        return true;
      }
    }

    // 如果权限被拒绝，显示提示
    SmartDialog.showToast(
      source == ImageSource.camera ? '需要相机权限才能拍照' : '需要相册权限才能选择图片',
    );

    return false;
  }

  // 处理图片选择
  Future<void> handleImagePicked(ImageSource source) async {
    try {
      // 先检查权限
      final hasPermission = await _checkPermission(source);
      if (!hasPermission) return;

      final picker.XFile? pickedFile = source == ImageSource.camera
          ? await _picker.pickImage(source: picker.ImageSource.camera)
          : await _picker.pickImage(source: picker.ImageSource.gallery);

      if (pickedFile != null) {
        images.add(pickedFile.path);
        update();
      }
    } catch (e) {
      Logger.log('Error picking image: $e');
      SmartDialog.showToast('选择图片失败，请重试');
    }
  }

  // 删除图片
  void deleteImage(int index) {
    if (index >= 0 && index < images.length) {
      // 删除本地文件
      final file = File(images[index]);
      if (file.existsSync()) {
        file.deleteSync();
      }
      images.removeAt(index);
      update();
    }
  }

  void updateSelectedAddress(String address) {
    selectedAddress = address;
    update();
  }
} 